package com.hcloud.uploadsdk;

import android.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public class AESBase64Util {

    private static String skey = "KrEj3WxoKitfRfkC";
    private static String siv = "4KewNGDcHa3Q4cTX";

    public static String encrypt(String sSrc) {
        try {
            if (skey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (skey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = skey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//"算法/模式/补码方式"
            //CBC模式需要配置偏移量，设置一个向量，达到密码唯一性，增加加密算法的强度
            IvParameterSpec iv = new IvParameterSpec(siv.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
            String result = new String(Base64.encode(encrypted, Base64.DEFAULT));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public static String decrypt(String sSrc) {
        try {
            // 判断Key是否正确
            if (skey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (skey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = skey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            //CBC模式需要配置偏移量，设置这个后，不会出来同一个明文加密为同一个密文的问题，达到密文唯一性
            IvParameterSpec iv = new IvParameterSpec(siv.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = Base64.decode(sSrc, Base64.DEFAULT);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, "utf-8");
            return originalString;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
}



